import { getSchemaTpl, setSchemaTpl } from 'amis-editor';
import { getEventControlConfig } from 'amis-editor/lib/renderer/event-control/helper';

// 重写模板
setSchemaTpl('formItemName', () => ({
  type: 'input-text',
  name: 'name',
  required: true,
  label: '字段名',
  trimContents: true,
}));

setSchemaTpl('selectFirst', null);

setSchemaTpl('commonTpl', (patch: any = {}) => {
  const { scope, context, groups = [] } = patch;
  const isFormItem = patch.isFormItem ?? scope?.isFormItem;
  const tabBody1: any[] = [];
  if (isFormItem) {
    tabBody1.push(getSchemaTpl('collapseGroup', [
      {
        title: '基本属性',
        body: [
          getSchemaTpl('formItemName', {
            required: true,
          }),
          {
            type: 'input-text',
            name: 'id',
            label: '组件ID',
          },
          getSchemaTpl('label'),
          ...getSchemaTpl('style:formItem', {}).body,
          getSchemaTpl('labelRemark'),
          getSchemaTpl('remark'),
          getSchemaTpl('placeholder'),
          getSchemaTpl('description'),
        ],
      },
    ]));
  }

  if (groups.length > 0) {
    groups.forEach((element: object | undefined) => {
      tabBody1.push(getSchemaTpl('collapseGroup', [element]));
    });
  }
  tabBody1.push(getSchemaTpl('extendProps'));

  const panels: Array<{ title: string, body: Array<any>, className?: string }> = [
    {
      title: '常规',
      body: tabBody1,
    },
    // 隐藏外观设置 后续有需要再开放
    // {
    //   title: '外观',
    //   body: [
    //     getSchemaTpl('collapseGroup', [
    //       ...getSchemaTpl('theme:common', {}),
    //     ]),
    //   ],
    // },
  ];

  if (scope?.events && context) {
    panels.push({
      title: '事件',
      className: 'p-none',
      body: [
        getSchemaTpl('eventControl', {
          name: 'onEvent',
          ...getEventControlConfig(scope.manager, context),
        }),
      ],
    });
  }

  return panels;
});

const disabledTplKeys = ['remark', 'labelRemark', 'selectFirst'];
disabledTplKeys.forEach((key) => setSchemaTpl(key, null));
